package org.cresource.user.service.impl;

import org.cresource.basic.constant.VerifyCodeContants;
import org.cresource.basic.exception.BusinessException;
import org.cresource.basic.service.impl.BaseServiceImpl;
import org.cresource.basic.util.MD5Utils;
import org.cresource.basic.util.StrUtils;
import org.cresource.org.domain.Employee;
import org.cresource.org.mapper.EmployeeMapper;
import org.cresource.user.domain.LoginInfo;
import org.cresource.user.domain.User;
import org.cresource.user.domain.UserDto;
import org.cresource.user.mapper.LoginInfoMapper;
import org.cresource.user.mapper.UserMapper;
import org.cresource.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * 用户s层
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    LoginInfoMapper loginInfoMapper;

    /**
     * 前端注册
     */
    @Override
    public void redisterCode(UserDto userDto) {
        //空校验
          if (StringUtils.isEmpty(userDto.getPhone())||
           StringUtils.isEmpty(userDto.getCode())||
           StringUtils.isEmpty(userDto.getPassword())||
           StringUtils.isEmpty(userDto.getPasswordRepeat())){
          throw new BusinessException("请输入完整的注册信息");
          }
        //两次密码验证
         if (!userDto.getPassword().equals(userDto.getPasswordRepeat())){
            throw new BusinessException("密码不一致，请重新输入！");
          }
        //是否被注  查询loginInfo是因为前端登录不需要看type
        LoginInfo login=loginInfoMapper.loadByDto(userDto);
        if (login!=null){
            throw new BusinessException("用户已被注册");
          }
        //查询验证码是否为空
        Object o = redisTemplate.opsForValue().get(VerifyCodeContants.USER_VERFIY_CODE + ":" + userDto.getPhone());
        //为空 证明验证码失效了
        if (o==null){
            throw new BusinessException("验证码失效咯");
        }
        String code = ((String) o).split(":")[0];
        //不为空 验证验证码是否一样
        if (!userDto.getCode().equalsIgnoreCase(code)){
            throw new BusinessException("验证码不对哦");
        }
        //密码正确 登录成功
        // 把数据加到对象中 添加数据
        User user = userDto2User(userDto);
        LoginInfo loginInfo = user2LoginInfo(user);
        //先加logininfo表 返回一个id
        loginInfoMapper.save(loginInfo);
        user.setInfo(loginInfo);
        userMapper.save(user);


    }

        //把前端传过来的前端注册信息 给一个user对象
    private User userDto2User(UserDto userDto) {
        //创建一个接收对象
        User user = new User();
        //传入基本值
        //这个值既是登录账号 又是电话
        user.setPhone(userDto.getPhone());
        user.setUsername(userDto.getPhone());
        //传入type
        user.setType(userDto.getType());
        //传入盐  取62个数中的32个随机数   62   52字母大小写 10阿拉伯数
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        //传入加密后密码
        String Md5Psw = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(Md5Psw);
        return user;
    }



        //把接收前端注册信息的user对象的信息
    private LoginInfo user2LoginInfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        //用工具类cope对象 第一个被copy
        BeanUtils.copyProperties(user, loginInfo);
        return loginInfo;
    }







}
